#include "midi_player_widget.h"

#include <QGroupBox>
#include <QHBoxLayout>
#include <QPushButton>
#include <QVBoxLayout>

#include "core/midi_player.h"

MidiPlayerWidget::MidiPlayerWidget(QWidget *parent):
  QWidget(parent) {
  midiPlayer = new MidiPlayer(this);
  connect(midiPlayer, SIGNAL(finished()), this, SLOT(ready()));
  connect(midiPlayer, SIGNAL(finished()), this, SIGNAL(stopped()));

  listenButton = new QPushButton("Écout&er", this);
  listenButton->setToolTip("Cliquez sur ce bouton "
                           "pour écouter la mélodie générée");
  connect(listenButton, SIGNAL(clicked()), this, SLOT(play()));

  stopButton = new QPushButton("&Stop", this);
  stopButton->setToolTip("Cliquez sur ce bouton pour "
                         "arrêter d'écouter la mélodie");
  connect(stopButton, SIGNAL(clicked()), this, SLOT(stop()));

  notReady();

  QGroupBox *midiBox = new QGroupBox("MIDI", this);
  QVBoxLayout *midiBoxLayout = new QVBoxLayout;
  midiBox->setLayout(midiBoxLayout);

  midiBoxLayout->addWidget(listenButton);
  midiBoxLayout->addWidget(stopButton);

  QHBoxLayout *mainLayout = new QHBoxLayout;
  mainLayout->addWidget(midiBox);
  setLayout(mainLayout);
}

/*!
 * Désactive les boutons du widget
 */
void MidiPlayerWidget::notReady() {
  listenButton->setDisabled(true);
  stopButton->setDisabled(true);
}

/*!
 * Active les boutons du widget
 */
void MidiPlayerWidget::ready() {
  listenButton->setEnabled(true);
  stopButton->setDisabled(true);
}

/*!
 * Lance la lecture du fichier MIDI
 */
void MidiPlayerWidget::play() {
  listenButton->setDisabled(true);
  stopButton->setEnabled(true);

  emit playing();
  midiPlayer->play();
}

/*!
 * Arrête la lecture du fichier MIDI
 */
void MidiPlayerWidget::stop() {
  midiPlayer->stop();
  emit stopped();
}
